from django.shortcuts import render,reverse
from django.http import HttpResponse,JsonResponse

from myadmin.models import Order,Address,OrderInfo

def orderlist(request):
    # 查询所有的订单
    orderall = Order.objects.all()      #订单对象
    # 查询出来对应的id
    for i in orderall:
        i.add = i.address.adds      # i.address就是对应的地址对象
    # print(orderall)

    content = {'orderall':orderall}
    return render(request,'admin/order/list.html',content)

def orderstatus(request):
    # print(request.GET.get('status'))        # 字符串
    #根据传值拿到订单
    orderobj = Order.objects.get(id=request.GET['Oid'])
    orderobj.status = int(request.GET.get('status'))
    orderobj.save()
    return  HttpResponse('')


def orderinfo(request,id):

    # 获取当前选择的订单id  .address  .uid 分别查寻地址和用户对象
    orderobj = Order.objects.get(id=id)
    infos = orderobj.orderinfo_set.all()

    content={'orderobj':orderobj,'infos':infos}

    return render(request,'admin/order/info.html',content)